import json
import uuid
from shapely.geometry import shape
from shapely.ops import unary_union
import fiona
from pyproj import Proj, transform
import numpy as np
 
def convert_to_3dtiles(input_file):
    # 打开输入的GeoJSON文件
    with open(input_file) as file:
        data = json.load(file)
    
    features = []
    for feature in data['features']:
        geometry = feature['geometry']
        
        if geometry['type'] == 'Polygon':
            polygon = shape(geometry)
            
            # 获取多边形的外接矩形
            bounds = polygon.bounds
            
            # 计算每个顶点在地理空间上的经度和纬度
            lonlat = [transform(Proj('EPSG:4326'), Proj('EPSG:3857'), point[0], point[1]) for point in zip(*polygon.exterior.xy)]
            
            # 创建3D Tile的Feature对象
            tile_feature = {
                "id": str(uuid.uuid4()),
                "properties": {},
                "boundingVolume": {
                    "region": [[lonlat[0][0], lonlat[-1][1]], [lonlat[-1][0], lonlat[0][1]]]
                },
                "content": {
                    "uri": "./tile"
                }
            }
            
            features.append(tile_feature)
    
    return features
 
# 调用函数并传入GeoJSON文件路径作为参数
result = convert_to_3dtiles("./allmap.json")
print(result)
